package hlhj.fhp.tvlib.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.flyco.tablayout.listener.OnTabSelectListener
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.utils.DensityUtil
import com.liaoinstan.springview.utils.DensityUtil.dip2px
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.tenma.ventures.bean.TMUser
import com.tenma.ventures.bean.utils.TMSharedPUtil
import com.tenma.ventures.config.TMServerConfig
import com.tenma.ventures.tools.TMAlgorithm
import com.tenma.ventures.tools.TMApp
import com.tenma.ventures.tools.TMSharedP
import com.tenma.ventures.tools.TMThemeManager
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.activitys.BroadCastAty
import hlhj.fhp.tvlib.activitys.LiveListAty
import hlhj.fhp.tvlib.activitys.TvAty
import hlhj.fhp.tvlib.adapter.LivePagerAdp
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.CatBean
import hlhj.fhp.tvlib.javabean.TvListBean
import hlhj.fhp.tvlib.utils.TabUtils
import kotlinx.android.synthetic.main.tv_home_new.view.*
import java.lang.reflect.Field
import java.util.ArrayList

/**
 * Created by Administrator on 2018\6\1 0001.
 */
class TvHomeFgm :Fragment() {
    private lateinit var rootView:View
    private lateinit var adp: BaseQuickAdapter<TvListBean.DataBean, BaseViewHolder>
    private val datas= ArrayList<TvListBean.DataBean>()
    private lateinit var loadingDialog: LoadingDialog

    private lateinit var pagerAdp:LivePagerAdp
    private var liveHomeFgm:Fragment?=null
    private var tvHomeFgm:Fragment?=null
    private var castHomeFgm:Fragment?=null
    private val fgmLists=ArrayList<Fragment>()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        rootView=inflater.inflate(R.layout.tv_home_new,null)
//        rootView=inflater.inflate(R.layout.activity_home_aty,null)
        loadingDialog= LoadingDialog(context!!)
        if (TMSharedPUtil.getTMThemeColor(context)!=null&&TMSharedPUtil.getTMThemeColor(context).length!=0){
            rootView.loTittle.setBackgroundColor(Color.parseColor(TMSharedPUtil.getTMThemeColor(context)))
        }
        //        模拟数据
      /*  val tmUser = TMUser()
        tmUser.member_id=63
        tmUser.member_code="1D0916EF9A29336083BFB0017C90EAEA"
        TMSharedPUtil.saveTMUser(context,tmUser)
//        TMSharedPUtil.save
        TMSharedPUtil.saveTMToken(context,"E6D210476B08B9DE9C8495B83CA5AFD1")*/
        rootView.viewPager.offscreenPageLimit=10
        getCat()

        rootView.viewPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {
            }
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                GSYVideoManager.releaseAllVideos()
            }
        })

        return rootView
    }

    private fun getCat() {
        OkGo.get<String>(Contents.BaseUrl+Contents.CAT)
                .execute(object :StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        val str = response?.body()
                        val catBean = Gson().fromJson(str, CatBean::class.java)
                        catBean.data.forEachIndexed { index, dataBean ->
                            when(dataBean.name){
                                "直播"->{
                                    liveHomeFgm=HomeLiveFgm()
                                    fgmLists.add(liveHomeFgm!!)
                                }
                                "电视"->{
                                    tvHomeFgm=HomeTvFgm()
                                    fgmLists.add(tvHomeFgm!!)
                                }
                                "广播"->{
                                    castHomeFgm= HomeCastNew()
                                    fgmLists.add(castHomeFgm!!)
                                }
                            }
                       }
                        pagerAdp= LivePagerAdp(childFragmentManager!!,fgmLists)
                        rootView.viewPager.adapter=pagerAdp
                        rootView.tablayout.setViewPager(rootView.viewPager)
                    }
                })
    }

    override fun onPause() {
        GSYVideoManager.releaseAllVideos()
        super.onPause()
    }

}